package com.tz.water.controller.user;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tz.water.bean.User;
import com.tz.water.config.AccessLimit;
import com.tz.water.dto.PageDTO;
import com.tz.water.mapper.*;
import com.tz.water.utils.MyPasswordEncoder;
import com.tz.water.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserLogin {

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    DeviceMapper yimiaoOrderMapper;

    @AccessLimit(times = 5)
    @PostMapping("/login")
    @ResponseBody
    public HashMap<String,Object> login (@RequestBody User user, HttpServletRequest request) {

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getName,user.getName());
        List<User> users  = userMapper.selectList(userQueryWrapper);
        if(users.size()>0){
            if(MyPasswordEncoder.domatches(user.getPassword(),  users.get(0).getPassword())){
                request.getSession().setAttribute("loginrole","user");
                request.getSession().setAttribute("loginid",users.get(0).getId());
                System.out.println("sessionid :" + request.getSession().getId());
                return ResponseUtils.success(users.get(0) );
            }
            return ResponseUtils.fail("账户或密码错误！");
        }
        return ResponseUtils.fail("用户不存在");
    }

    @AccessLimit(times = 5)
    @PostMapping("/regist")
    @ResponseBody
    public HashMap<String,Object> regist (@RequestBody User user, HttpServletRequest request) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lambda().eq(User::getName,user.getName());
        List<User> users  = userMapper.selectList(userQueryWrapper);
        if(users.size()>0){
            return ResponseUtils.fail("用户已存在！");
        }
        user.setPassword(MyPasswordEncoder.doencode(user.getPassword()));
        userMapper.insert(user);
        return ResponseUtils.success("用户创建成功！");
    }



    @AccessLimit(times = 5)
    @PostMapping("/logout")
    public HashMap<String,Object> logout (@RequestBody User user, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") != null && request.getSession().getAttribute("loginrole").equals("user")) {
            request.getSession().setAttribute("loginrole",null);
            return ResponseUtils.fail("login out success ");
        }
        return ResponseUtils.notlogin("login fail ");
    }


    @AccessLimit(times = 5)
    @PostMapping("/changepass")
    public HashMap<String,Object> changepass (@RequestBody User user, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") != null && request.getSession().getAttribute("loginrole").equals("user")){
            long uid = (long) request.getSession().getAttribute("loginid");

            MyPasswordEncoder passwordEncoder = new MyPasswordEncoder();
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            user.setId(uid);
            if(userMapper.updateById(user) >0){
                return ResponseUtils.success("change success");
            }
        }
        return ResponseUtils.notlogin("login fail");
    }

    @AccessLimit(times = 5)
    @PostMapping("/getLastLog")
    public HashMap<String,Object> getLastLog (@RequestBody User user, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") != null && request.getSession().getAttribute("loginrole").equals("user")){
            long uid = (long) request.getSession().getAttribute("loginid");

            MyPasswordEncoder passwordEncoder = new MyPasswordEncoder();
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            user.setId(uid);
            if(userMapper.updateById(user) >0){
                return ResponseUtils.success("change success");
            }
        }
        return ResponseUtils.notlogin("login fail");
    }
}
